package srx.open.com.activity;

import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.update.AppVersion;
import de.greenrobot.event.EventBus;
import srx.open.com.R;
import srx.open.com.bean.Logout;
import srx.open.com.bean.User;
import srx.open.com.utils.APIUtil;
import srx.open.com.utils.DataCleanManagerUtils;
import srx.open.com.utils.OpenToast;
import srx.open.com.utils.UserUtils;

/**
 * 设置界面.
 *
 * @author hailongqiu 356752238@qq.com
 */
public class SettingsActivity extends ExtendBaseActivity {

    // 标题栏
    @Bind(R.id.left_lay)
    View left_lay; // 返回.
    @Bind(R.id.left_tv)
    TextView left_tv; // 左边的标题文本.
    @Bind(R.id.right_lay)
    View right_lay; // 设置.

    // 版本更新.
    @Bind(R.id.update_show_tv)
    TextView update_show_tv;

    @Bind(R.id.logout_btn)
    Button logout_btn;

    int updateVersionCode = -1;
    String updateVersionName = "";

    @Override
    public void initAllDatas() {
    }

    @Override
    public void initAllViews() {
        EventBus.getDefault().register(this); // 注册事件.
        setContentView(R.layout.activity_settings);
        ButterKnife.bind(this);
        // 初始化标题栏.
        right_lay.setVisibility(View.GONE);
        left_tv.setText("设置");
        // 初始化退出按钮
        User user = UserUtils.getCurrentUser(mContext);
        onEvent(user);
        // 更新版本信息.
        String versionName = APIUtil.getVersion(mContext);
        int versionCode = APIUtil.getVersionCode(mContext);
        update_show_tv.setText("当前版本 v" + versionName);
        //
        BmobQuery<AppVersion> appVersionQuery = new BmobQuery<AppVersion>();
        appVersionQuery.addWhereGreaterThan("version_i", versionCode);
        appVersionQuery.findObjects(mContext, new FindListener<AppVersion>() {
            @Override
            public void onSuccess(List<AppVersion> list) {
                if (list.size() > 0) {
                    AppVersion appVersion = list.get(0);
                    updateVersionCode = appVersion.getVersion_i();
                    updateVersionName = appVersion.getVersion();
                    update_show_tv.setText("有新版本 v" + updateVersionName);
                }
            }

            @Override
            public void onError(int state, String msg) {
            }
        });
    }

    /*
     * 返回.
     */
    @OnClick(R.id.left_lay)
    void left_lay(View view) {
        finish();
    }

    /**
     * 更新检测.
     */
    @OnClick(R.id.check_update_lay)
    void check_update_lay(View view) {
        checkAppUpload();
    }

    /**
     * 清理缓存
     */
    @OnClick(R.id.clean_cache_lay)
    void clean_cache_lay(View view) {
        DataCleanManagerUtils.cleanInternalCache(mContext);
        DataCleanManagerUtils.cleanExternalCache(mContext);
        DataCleanManagerUtils.cleanAppCacheFiles();
        DataCleanManagerUtils.cleanAppImgsFiles();
        OpenToast.showMsg("清理完成");
    }

    /**
     * 反馈界面.
     */
    @OnClick(R.id.feed_back_lay)
    void feed_back_lay(View view) {
        openActivity(FeedBackMsgActivity.class);
    }

    @OnClick(R.id.logout_btn)
    void logout_btn(View view) {
        BmobUser.logOut(this);   //清除缓存用户对象  // 退出登录
        OpenToast.showMsg("当前用户退出");
        // 初始化退出按钮
        User user = UserUtils.getCurrentUser(mContext);
        onEvent(user);
        EventBus.getDefault().post(new Logout());
        finish();
    }

    /**
     * 关于界面.
     */
    @OnClick(R.id.about_lay)
    void about_lay(View view) {
        openActivity(AboutActivity.class);
    }

    /**
     * 接收更新的信号.
     */
    public void onEvent(User user) {
        if (user != null) { // 登录更新.
            logout_btn.setVisibility(View.VISIBLE);
        } else { // 退出登录.更新.
            logout_btn.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this); // 注销事件.
    }

}
